package com.lepu.app.fun.mother_circle;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.AppEnterTongJi;
import com.app.utils.BitmapLoadUtil;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.bean.BeanMotherCircle;
import com.lepu.app.fun.mother_circle.fragment.FragmentMotherTopic;
import com.lepu.app.fun.my.MySearchActivity;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class MotherTopicActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,CustomTopBarNew.OnTopbarNewRightButtonListener{
	private ViewPager mViewPager = null;
	private ImageView mAddImageView = null;
	
	private BeanMotherCircle mMotherCircle = null;
	
	private static MotherTopicActivity instance;
	
	public ArrayList<Fragment> fragmentList;
	
	private ArrayList<LinearLayout> mTabLayoutList;
	private ArrayList<ImageView> mTabPointList;
	
	public static MotherTopicActivity getInstance(){
		return instance;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mother_topic_activity);

		instance = this;
		UmengHelper.openActivityDurationTrack();
		init();
	}
	
	@Override
	public void onResume() {
		super.onResume();

		UmengHelper.OnPageStart(String.valueOf(getTongJiId()));
	}

	@Override
	public void onPause() {
		super.onPause();

		UmengHelper.OnPageEnd(String.valueOf(getTongJiId()));
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		instance = null;
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		Intent intentSearch = new Intent(this,
				MySearchActivity.class);
		intentSearch.putExtra("type", MySearchActivity.TYPE_TOPIC);
		startActivity(intentSearch, true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		topbar.setRightButton(R.drawable.mother_search_post_icon);

		Bundle data = this.getIntent().getExtras();
		if(data != null && data.size() > 0)
		{
			mMotherCircle=(BeanMotherCircle)data.getSerializable("motherCircle");
		}

		if(mMotherCircle == null)
		{
			return;
		}

		AppEnterTongJi.clickEvent(getTongJiId(), mMotherCircle.cricleID);

		topbar.setTopbarTitle(mMotherCircle.cricleName);
		
		//head
		RelativeLayout mTopicHeadLayout = (RelativeLayout) findViewById(R.id.topic_head_layout);
		ImageView mTopicHeadAvatat = (ImageView) findViewById(R.id.topic_head_avatar);
		TextView mTopicHeadTitle = (TextView) findViewById(R.id.topic_head_title);
		TextView mTopicHeadContent = (TextView) findViewById(R.id.topic_head_content);
		TextView mTopicHeadPeople = (TextView) findViewById(R.id.peopleCountTextView);
		TextView mTopicHeadTopic = (TextView) findViewById(R.id.topicCountTextView);
		
		mTopicHeadTitle.setText(mMotherCircle.cricleName);
		mTopicHeadContent.setText(mMotherCircle.cricleContent);
		mTopicHeadPeople.setText(mMotherCircle.criclePeopleCount+" 加入");
		mTopicHeadTopic.setText(mMotherCircle.cricleTopicCount + " 话题");
		
		//UniversalImageLoadTool.disPlay(mMotherCircle.cricleAvatar, mTopicHeadAvatat, R.drawable.default_icon_small);
		BitmapLoadUtil.displayImage(R.drawable.default_icon_small, mMotherCircle.cricleAvatar, mTopicHeadAvatat);

		mTopicHeadLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(MotherTopicActivity.this, MotherCircleDetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("motherCircle", mMotherCircle);
				intent.putExtras(bundle);
				startActivity(intent,true);
			}
		});
		
		//tap
		mTabLayoutList = new ArrayList<LinearLayout>();
		mTabPointList = new ArrayList<ImageView>();
		
		mTabLayoutList.add( (LinearLayout) findViewById(R.id.tabLayout1));
		mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout2));
		mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout3));
		
		mTabPointList.add((ImageView) findViewById(R.id.tabImage1));
		mTabPointList.add((ImageView) findViewById(R.id.tabImage2));
		mTabPointList.add((ImageView) findViewById(R.id.tabImage3));
		
		//layout click
		for(int i=0; i<mTabLayoutList.size(); i++){
			mTabLayoutList.get(i).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					for(int k=0; k<mTabLayoutList.size(); k++){
						if(mTabLayoutList.get(k).getId() == v.getId()){
							mTabLayoutList.get(k).setSelected(true);
							mTabPointList.get(k).setVisibility(View.VISIBLE);
							mViewPager.setCurrentItem(k,false);
						}else{
							mTabLayoutList.get(k).setSelected(false);
							mTabPointList.get(k).setVisibility(View.INVISIBLE);
							
						}
					}
				}
			});
		}
		
		mTabLayoutList.get(0).setSelected(true);
		mTabPointList.get(0).setVisibility(View.VISIBLE);
		
		//viewPager
		fragmentList = new ArrayList<Fragment>();
		FragmentMotherTopic topic1 = new FragmentMotherTopic();
		FragmentMotherTopic topic2 = new FragmentMotherTopic();
		FragmentMotherTopic topic3 = new FragmentMotherTopic();
		
		Bundle data1 = new Bundle();
		data1.putInt("Type", 1);
		data1.putSerializable("MotherCircle", mMotherCircle);
		topic1.setArguments(data1);
		
		Bundle data2 = new Bundle();
		data2.putInt("Type", 2);
		data2.putSerializable("MotherCircle", mMotherCircle);
		topic2.setArguments(data2);
		
		Bundle data3 = new Bundle();
		data3.putInt("Type", 3);
		data3.putSerializable("MotherCircle", mMotherCircle);
		topic3.setArguments(data3);
		
		fragmentList.add(topic1);
		fragmentList.add(topic2);
		fragmentList.add(topic3);
		
		mViewPager = (ViewPager)findViewById(R.id.motherCircleListViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(this.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		mViewPager.setOffscreenPageLimit(2);
		
		//
		mAddImageView = (ImageView) findViewById(R.id.motherCircleListAddImageView);
		mAddImageView.setOnClickListener(mViewClickListener);

		//ObserverManager.getInstance().addObserver(ObserverConst.NOTIFY_MOTHER_TOPIC_CHANGE, this);
	}
	
	public int getTongJiId()
	{
		if(mMotherCircle == null)
		{
			return 0;
		}
		
		int id = 0;
		
		int circleId = UtilityBase.parseInt(mMotherCircle.cricleID);
		
		switch (circleId) {
		case 2:
			id = 6;
			break;
		case 3:
			id = 7;
			break;
		case 4:
			id = 5;
			break;
		case 5:
			id = 8;
			break;
		case 6:
			id = 9;
			break;
		case 7:
			id = 10;
			break;
		case 13:
			id = 11;
			break;
		case 17:
			id = 12;
			break;
		case 18:
			id = 13;
			break;
		case 19:
			id = 14;
			break;
		case 20:
			id = 15;
			break;
		case 21:
			id = 16;
			break;
		case 22:
			id = 17;
			break;
		case 23:
			id = 18;
			break;
		case 24:
			id = 19;
			break;
		case 25:
			id = 20;
			break;
		case 26:
			id = 21;
			break;

		default:
			break;
		}
		
		return id;
	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.motherCircleListAddImageView:
				
				AppEnterTongJi.clickEvent(49, "");
				
				boolean flagLogin = MainTabActivity.jumpLogin(MotherTopicActivity.this);
				
				if(flagLogin){
					return;
				}
				
				Intent intent = new Intent(MotherTopicActivity.this,MotherPubContentActivity.class);
				boolean flag = false;
				if(mMotherCircle.cricleID.equals("2")){
					flag = true;
				}
				
				intent.putExtra("IsBabayShow", flag);
				intent.putExtra("CircleId", mMotherCircle.cricleID);
				startActivity(intent, true);
				break;
			default:
				break;
			}
		}
	};

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mViewPager.setCurrentItem(index,false);
			
			for(int i=0; i<mTabLayoutList.size(); i++){
				if(i == index){
					mTabLayoutList.get(i).setSelected(true);
					mTabPointList.get(i).setVisibility(View.VISIBLE);
				}else{
					mTabLayoutList.get(i).setSelected(false);
					mTabPointList.get(i).setVisibility(View.INVISIBLE);
				}
			}
		}
	}

//	@SuppressWarnings("static-access")
//	@Override
//	public void notify(String arg0, Object arg1, Object arg2) {
//		for(int i=0; i<fragmentList.size(); i++){
//			((FragmentMotherTopic)fragmentList.get(i)).getInstance().request(false);
//		}
//	}

}
